package cn.com.yto56.coresystem.business.msg.soa.biz.impl;

import cn.com.yto56.coresystem.business.msg.soa.biz.IUserGroupInfoBiz;
import cn.com.yto56.coresystem.common.msg.util.base.StringUtils;
import cn.com.yto56.coresystem.common.msg.util.domain.AppResult;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IUserGroupInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.dao.IUserInfoDao;
import cn.com.yto56.coresystem.logic.msg.soa.domain.UserGroupInfoBean;
import cn.com.yto56.coresystem.logic.msg.soa.domain.UserInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Map;

/**
 * Created by wangrui on 2017/2/17.
 */
@Service
public class UserGroupInfoBizImpl implements IUserGroupInfoBiz {

    @Autowired
    private IUserGroupInfoDao userGroupInfoDaoImpl;
    @Autowired
    private IUserInfoDao userInfoDaoImpl;

    @Transactional
    @Override
    public AppResult addUserToGroup(String sysInfo_Id, String userGroupId, String userId) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userGroupId)) {
            resultMessage = "参数[yto_msg_userGroupId]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            //该用户是否合法
            UserInfoBean userInfoBean = new UserInfoBean();
            userInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
            userInfoBean.setYto_msg_userid(userId);
            userInfoBean.setStatus("VALID");
            boolean checkUpdate = userInfoDaoImpl.searchForUpdate(userInfoBean);
            if (checkUpdate) {
                resultMessage = "用户[" + userId + "]不存在，或已被删除";
            } else {
                //该组中是否存在该用户
                UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
                userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
                userGroupInfoBean.setYto_msg_usergroupid(userGroupId);
                userGroupInfoBean.setYto_msg_userid(userId);
                userGroupInfoBean.setStatus("VALID");
                checkUpdate = userGroupInfoDaoImpl.searchForUpdate(userGroupInfoBean);
                if (!checkUpdate) {
                    resultMessage = "组[" + userGroupId + "]中已经存在用户[" + userId + "]";
                } else {
                    userGroupInfoBean.setCreate_time(new Date());
                    int t = userGroupInfoDaoImpl.add(userGroupInfoBean);
                    if (t <= 0) {
                        resultMessage = "[系统错误]-sql执行失败";
                    } else {
                        resultFlag = true;
                        resultMessage = "执行成功";
                    }
                }
            }

        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }

    @Override
    public AppResult deleteUserFromGroup(String sysInfo_Id, String userGroupId, String userId) {
        AppResult appResult = new AppResult();
        boolean resultFlag = false;
        String resultMessage = "";
        Map datas = null;
        if (StringUtils.isBlank(sysInfo_Id)) {
            resultMessage = "参数[yto_msg_sysInfo_Id]不能为空";
        } else if (StringUtils.isBlank(userGroupId)) {
            resultMessage = "参数[yto_msg_userGroupId]不能为空";
        } else if (StringUtils.isBlank(userId)) {
            resultMessage = "参数[yto_msg_userId]不能为空";
        } else {
            UserGroupInfoBean userGroupInfoBean = new UserGroupInfoBean();
            userGroupInfoBean.setYto_msg_sysinfo_id(sysInfo_Id);
            userGroupInfoBean.setYto_msg_usergroupid(userGroupId);
            userGroupInfoBean.setYto_msg_userid(userId);
            userGroupInfoBean.setStatus("VALID");
            boolean checkUpdate = userGroupInfoDaoImpl.searchForUpdate(userGroupInfoBean);
            if (checkUpdate) {
                resultMessage = "组[" + userGroupId + "]中不存在用户[" + userId + "]，或已经被删除";
            } else {
                int t = userGroupInfoDaoImpl.delete(userGroupInfoBean);
                if (t <= 0) {
                    resultMessage = "[系统错误]-sql执行失败";
                } else {
                    resultFlag = true;
                    resultMessage = "执行成功";
                }
            }
        }

        appResult.setResultFlag(resultFlag);
        appResult.setResultMessage(resultMessage);
        appResult.setDatas(datas);
        return appResult;
    }
}
